/*
 * category-view.c
 *
 *  Created on: 22-dic-2008
 *      Author: igor
 */

#include "view.h"


/* aggiunge una nuova categoria
 *
 * adds a new category
 */
static void
my_entry_bt_new_cat(void *data, Evas_Object *obj, void *event_info)
{

	printf ("DEBUG: <my_entry_bt_new_cat>\n");
	   Evas_Object *tdi = data;
	   char *name = (char *)elm_entry_entry_get(data);

	   // Remove trailing "<br>"
	   if(strlen(name) >= 4 && !strncmp(name + (strlen(name)-4), "<br>", 4)) {
	       name[strlen(name) - 4] = '\0';
	   }

	   add_cat_into_db(name); // restituisce NULL se qualcosa non va per il verso giusto
//	   sprintf(catname, name);

	   // Esce dalla funzione
	   return;

}

/* prepara i dati per una nuova categoria
 *
 * prepares data for a new category
 */
static void
my_win_new_cat(void *data, Evas_Object *obj, void *event_info)
{
   printf ("DEBUG: <my_win_new_cat>\n");

   Evas_Object *win, *bg, *bx, *bx2, *bt, *tit, *txt, *en, *sc_tit, *ly;
   char buf[PATH_MAX];
   Evas_Object *mother = data;	// parent window

   win = elm_win_add(NULL, PACKAGE " new category", ELM_WIN_BASIC);
   elm_win_title_set(win, PACKAGE " - new category");
   elm_win_autodel_set(win, 1);

   bg = elm_bg_add(win);
   elm_win_resize_object_add(win, bg);
   evas_object_size_hint_weight_set(bg, 1.0, 1.0);
   evas_object_show(bg);

   ly = elm_layout_add(win);
   snprintf(buf, sizeof(buf), "%s/%s.edj", PIXMAPS_DIR, PACKAGE);
   printf ("DEBUG layout: %s;\n", buf);
   elm_layout_file_set(ly, buf, "add_cat");
   elm_win_resize_object_add(win, ly);
   evas_object_show(ly);

   bx = elm_box_add(win);
   window_swallow(ly, "contenitore_bx", bx);
   evas_object_size_hint_weight_set(bx, 1.0, 1.0);
   evas_object_resize(bx, 320, 300);
   evas_object_show(bx);

   sc_tit = elm_scroller_add(win);
   window_swallow(ly, "title_entry_sc", sc_tit);
   elm_scroller_content_min_limit(sc_tit, 0, 1);
   evas_object_size_hint_weight_set(sc_tit, 1.0, 0.0);
   evas_object_size_hint_align_set(sc_tit, -1.0, -1.0);
   elm_box_pack_end(bx, sc_tit);
   evas_object_show(sc_tit);

   tit = elm_entry_add(win);
   window_swallow(ly, "title_entry", tit);
//   elm_entry_line_wrap_set(tit, 0);
   elm_entry_entry_set(tit, "");
   evas_object_size_hint_weight_set(tit, 1.0, 0.0);
//   elm_entry_select_all(tit);
//   elm_box_pack_end(bx, tit);
   elm_scroller_content_set(sc_tit, tit);
   evas_object_show(tit);

   bx2 = elm_box_add(win);
   window_swallow(ly, "footer_bx", bx2);
   elm_box_horizontal_set(bx2, 1);
   evas_object_size_hint_weight_set(bx2, 1.0, 1.0);
   evas_object_show(bx2);

   Evas_Object *p = NULL;	// name of the new category
   p = tit;

   bt = elm_button_add(win);
   window_swallow(ly, "button_save", bt);
   elm_button_label_set(bt, "Save");
   // se esegue prima la chiusura poi non salva
   evas_object_smart_callback_add(bt, "clicked", my_win_close, win);
   evas_object_smart_callback_add(bt, "clicked", my_entry_bt_changecat, mother);
   evas_object_smart_callback_add(bt, "clicked", my_entry_bt_new_cat, p);
   evas_object_size_hint_weight_set(bt, 1.0, 1.0);
   elm_box_pack_end(bx2, bt);
   evas_object_show(bt);

   evas_object_resize(win, 320, 300);

   // FIXME: not exported
   elm_widget_focus_set(win, 1);
   evas_object_show(win);

   printf ("DEBUG: </my_win_new_cat>\n");
}

/* modifica la categoria visualizzata
 *
 * changes displayed category
 */
static void
my_bt_modify_cat(void *data, Evas_Object *obj, void *event_info)
{
	printf ("DEBUG: <my_bt_modify_cat>\n");

	sprintf ((char *)catname, "%s", (const char *)data);

	printf ("DEBUG: </my_bt_modify_cat>\n");
}

/*
 * delete displayed category
 */
static void
my_bt_delete_cat(void *data, Evas_Object *obj, void *event_info)
{
	printf ("DEBUG: <my_bt_delete_cat>\n");

	del_cat_from_db((const char *)data); // TODO - if NULL is ret value, there was a problem

	printf ("DEBUG: </my_bt_delete_cat>\n");
}

/* cambia la categoria visualizzata
 *
 * changes the displayed category
 */
void
my_entry_bt_changecat(void *data, Evas_Object *obj, void *event_info)
{
   printf ("DEBUG: <my_entry_bt_changecat>\n");

   Evas_Object *win, *bg, *bx0, *lb, *bx, *bt, *sc, *fr, *ly, *sc_tit;
   char buf[PATH_MAX];
   Evas_Object *mother = data;		// parent win

   win = elm_win_add(NULL, PACKAGE " cat change", ELM_WIN_BASIC);
   /* set the title of the window - this is in the titlebar */
   elm_win_title_set(win, PACKAGE " - cat change");
   elm_win_autodel_set(win, 1);

   bg = elm_bg_add(win);
   /* set weight to 1.0 x 1.0 == expand in both x and y direction */
   evas_object_size_hint_weight_set(bg, 1.0, 1.0);
   /* tell the window that this object is to be resized along with the window.
    * also as a result this object will be one of several objects that
    * controls the minimum/maximum size of the window */
   elm_win_resize_object_add(win, bg);
   /* and show the background */
   evas_object_show(bg);

   ly = elm_layout_add(win);
   snprintf(buf, sizeof(buf), "%s/%s.edj", PIXMAPS_DIR, PACKAGE);
   printf ("DEBUG layout: %s;\n", buf);
   elm_layout_file_set(ly, buf, "change_cat");
   elm_win_resize_object_add(win, ly);
   evas_object_show(ly);

   /* add a box layout widget to the window */
   bx0 = elm_box_add(win);
   window_swallow(ly, "title_bx", bx0);
   /* allow base box (bx0) to expand in x and y */
   evas_object_size_hint_weight_set(bx0, 1.0, 1.0);
   /* tell the window that the box affects window size and also will be
    * resized when the window is */
   elm_win_resize_object_add(win, bx0);
   evas_object_show(bx0);

   fr = elm_frame_add(win);
   window_swallow(ly, "title_frame", fr);
   elm_frame_label_set(fr, "Categories");
   elm_box_pack_end(bx0, fr);
   evas_object_show(fr);

   lb = elm_label_add(win);
   elm_label_label_set(lb,
      "select a category from the list<br>"
      "below by clicking the button to<br>"
      "select it, or chose 'New' button to"
	  "<br>add a new category to the list");
   elm_frame_content_set(fr, lb);
   evas_object_show(lb);

   /* add a scroller object - anything inside is scrollable */
   sc_tit = elm_scroller_add(win);
   window_swallow(ly, "title_sc", sc_tit);
   evas_object_size_hint_weight_set(sc_tit, 1.0, 1.0);
   elm_box_pack_end(bx0, sc_tit);
   evas_object_show(sc_tit);

   /* add a scroller object - anything inside is scrollable */
   sc = elm_scroller_add(win);
   window_swallow(ly, "contenitore_sc", sc);
   /* add scroller to main box */
   evas_object_size_hint_weight_set(sc, 1.0, 1.0);
   evas_object_show(sc);

   /* add a box layout widget to the window */
   bx = elm_box_add(win);
   /* set weight to 1.0 x 1.0 == expand in x and y) */
   evas_object_size_hint_weight_set(bx, 1.0, 0.0);

   /* set the box ad the content in the scrolled view*/
   elm_scroller_content_set(sc, bx);
   /* show the box, scroller and main box */
   evas_object_show(bx);

   struct charlist *catlist = NULL;
   catlist = (struct charlist *) leggi_cat_da_db(catlist);

   if (catlist == NULL)
   {
	      printf("DEBUG: lista vuota;\n");
   } else {

      int i = 0;
	  while (catlist != NULL)
	  {
         bt = elm_button_add(win);
         elm_button_label_set(bt, catlist->inf);
         evas_object_smart_callback_add(bt, "clicked", my_win_close, win);
         evas_object_smart_callback_add(bt, "clicked", my_entry_bt_refresh, mother);
         evas_object_smart_callback_add(bt, "clicked", my_bt_modify_cat, catlist->inf);

         /* the button should fill any space it is given horizontally */
         evas_object_size_hint_align_set(bt, -1.0, 0.0);
         /* put the button at the end of the box */
         elm_box_pack_end(bx, bt);
         /* show the button */
	     evas_object_show(bt);

	     // elemento successivo
	     catlist = catlist->pun;

      }
   }

   Evas_Object *bt_new, *bt_quit;
   bt_new = elm_button_add(win);
   window_swallow(ly, "button_add", bt_new);
   elm_button_label_set(bt_new, "New");
   evas_object_smart_callback_add(bt_new, "clicked", my_win_close, win);
//   evas_object_smart_callback_add(bt_new, "clicked", my_entry_bt_refresh, mother);
   evas_object_smart_callback_add(bt_new, "clicked", my_win_new_cat, mother);
   evas_object_size_hint_align_set(bt_new, -1.0, 0.0);
   evas_object_show(bt_new);

   /* create a button */
   bt_quit = elm_button_add(win);
   window_swallow(ly, "button_quit", bt_quit);
   /* set the button label */
   elm_button_label_set(bt_quit, "Quit");
   /* add a callback to be run when the button is clicked */
   evas_object_smart_callback_add(bt_quit, "clicked", my_win_close, win);
   evas_object_smart_callback_add(bt_quit, "clicked", my_entry_bt_refresh, mother);

   /* the button should fill any space it is given horizontally */
   evas_object_size_hint_align_set(bt_quit, -1.0, 0.0);
   /* show the button */
   evas_object_show(bt_quit);

   /* set an initial window size */
   evas_object_resize(win, 320, 300);
   /* show the window */
   evas_object_show(win);

   printf ("DEBUG: </my_entry_bt_changecat>\n");
}

/*
 * delete a category from a list, a cat to be deleted must be empty: it have
 * to contain zero or less items.
 *
 */
void
my_entry_bt_deletecat(void *data, Evas_Object *obj, void *event_info)
{
   printf ("DEBUG: <my_entry_bt_deletecat>\n");

   Evas_Object *win, *bg, *bx0, *lb, *bx, *bt, *sc, *fr, *ly, *sc_tit;
   char buf[PATH_MAX];
   Evas_Object *mother = data;		// parent win

   win = elm_win_add(NULL, PACKAGE " cat del", ELM_WIN_BASIC);
   /* set the title of the window - this is in the titlebar */
   elm_win_title_set(win, PACKAGE " - cat del");
   elm_win_autodel_set(win, 1);

   bg = elm_bg_add(win);
   /* set weight to 1.0 x 1.0 == expand in both x and y direction */
   evas_object_size_hint_weight_set(bg, 1.0, 1.0);
   /* tell the window that this object is to be resized along with the window.
    * also as a result this object will be one of several objects that
    * controls the minimum/maximum size of the window */
   elm_win_resize_object_add(win, bg);
   /* and show the background */
   evas_object_show(bg);

   ly = elm_layout_add(win);
   snprintf(buf, sizeof(buf), "%s/%s.edj", PIXMAPS_DIR, PACKAGE);
   printf ("DEBUG layout: %s;\n", buf);
   elm_layout_file_set(ly, buf, "delete_cat");
   elm_win_resize_object_add(win, ly);
   evas_object_show(ly);

   /* add a box layout widget to the window */
   bx0 = elm_box_add(win);
   window_swallow(ly, "title_bx", bx0);
   /* allow base box (bx0) to expand in x and y */
   evas_object_size_hint_weight_set(bx0, 1.0, 1.0);
   /* tell the window that the box affects window size and also will be
    * resized when the window is */
   elm_win_resize_object_add(win, bx0);
   evas_object_show(bx0);

   fr = elm_frame_add(win);
   window_swallow(ly, "title_frame", fr);
   elm_frame_label_set(fr, "Categories");
   elm_box_pack_end(bx0, fr);
   evas_object_show(fr);

   lb = elm_label_add(win);
   elm_label_label_set(lb,
      "select a category from the list<br>"
      "below by clicking the button to"
	  "<br>delete the category.");
   elm_frame_content_set(fr, lb);
   evas_object_show(lb);

   /* add a scroller object - anything inside is scrollable */
   sc_tit = elm_scroller_add(win);
   window_swallow(ly, "title_sc", sc_tit);
   evas_object_size_hint_weight_set(sc_tit, 1.0, 1.0);
   elm_box_pack_end(bx0, sc_tit);
   evas_object_show(sc_tit);

   /* add a scroller object - anything inside is scrollable */
   sc = elm_scroller_add(win);
   window_swallow(ly, "contenitore_sc", sc);
   /* add scroller to main box */
   evas_object_size_hint_weight_set(sc, 1.0, 1.0);
   evas_object_show(sc);

   /* add a box layout widget to the window */
   bx = elm_box_add(win);
   /* set weight to 1.0 x 1.0 == expand in x and y) */
   evas_object_size_hint_weight_set(bx, 1.0, 0.0);

   /* set the box ad the content in the scrolled view*/
   elm_scroller_content_set(sc, bx);
   /* show the box, scroller and main box */
   evas_object_show(bx);

   struct charlist *catlist = NULL;
   catlist = (struct charlist *) leggi_cat_da_db(catlist);

   if (catlist == NULL)
   {
	      printf("DEBUG: lista vuota;\n");
   } else {

      int i = 0;
	  while (catlist != NULL)
	  {
         bt = elm_button_add(win);
         elm_button_label_set(bt, catlist->inf);
         evas_object_smart_callback_add(bt, "clicked", my_win_close, win);
         evas_object_smart_callback_add(bt, "clicked", my_entry_bt_refresh, mother);
         evas_object_smart_callback_add(bt, "clicked", my_bt_delete_cat, catlist->inf);

         /* the button should fill any space it is given horizontally */
         evas_object_size_hint_align_set(bt, -1.0, 0.0);
         /* put the button at the end of the box */
         elm_box_pack_end(bx, bt);
         /* show the button */
	     evas_object_show(bt);

	     // elemento successivo
	     catlist = catlist->pun;

      }
   }

   Evas_Object *bt_quit;

   /* create a button */
   bt_quit = elm_button_add(win);
   window_swallow(ly, "button_quit", bt_quit);
   /* set the button label */
   elm_button_label_set(bt_quit, "Quit");
   /* add a callback to be run when the button is clicked */
   evas_object_smart_callback_add(bt_quit, "clicked", my_win_close, win);
   evas_object_smart_callback_add(bt_quit, "clicked", my_entry_bt_refresh, mother);

   /* the button should fill any space it is given horizontally */
   evas_object_size_hint_align_set(bt_quit, -1.0, 0.0);
   /* show the button */
   evas_object_show(bt_quit);

   /* set an initial window size */
   evas_object_resize(win, 320, 300);
   /* show the window */
   evas_object_show(win);

   printf ("DEBUG: </my_entry_bt_deletecat>\n");
}
